/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.jy.springbootcmsservice.common.base.entity;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.jy.springbootcmsservice.common.utils.IdGen;
import com.jy.springbootcmsservice.common.utils.UserUtils;
import com.jy.springbootcmsservice.modules.sys.entity.User;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

/**
 * 数据Entity类
 * @author ThinkGem
 * @version 2014-05-16
 */
@JsonInclude
public abstract class DataEntity<T> extends BaseEntity<T> {

	private static final long serialVersionUID = 1L;

	/**
	 * 备注
	 */
	protected String remarks;
	/**
	 * 创建者
	 */
	protected User createBy;
	/**
	 * 创建日期
	 */
	protected Date createDate;
	/**
	 * 更新者
	 */
	protected User updateBy;
	/**
	 * 更新日期
	 */
	protected Date updateDate;
	/**
	 * 删除标记（0：正常；1：删除；2：审核）
	 */
	protected String deleteFlag;
	
	public DataEntity() {
		super();
		this.deleteFlag = DEL_FLAG_NORMAL;
	}
	
	public DataEntity(String id) {
		super(id);
	}
	
	/**
	 * 插入之前执行方法，需要手动调用
	 */
	@Override
	public void preInsert(){
		// 不限制ID为UUID，调用setIsNewRecord()使用自定义ID
		if (!this.isNewRecord){
			setId(IdGen.uuid());
		}
		User user = UserUtils.getUser();
		if (StringUtils.isNotBlank(user.getId())){
			this.updateBy = user;
			this.createBy = user;
		}
		this.updateDate = new Date();
		this.createDate = this.updateDate;
	}
	
	/**
	 * 更新之前执行方法，需要手动调用
	 */
	@Override
	public void preUpdate(){
		User user = UserUtils.getUser();
		if (StringUtils.isNotBlank(user.getId())){
			this.updateBy = user;
		}
		this.updateDate = new Date();
	}

}
